/**
 * Created by hjx on 2019/1/4.
 */
(function (exports) {

    var $ = null;

    var isArraySame = function (a1, a2) {
        if (a1 == null || a2 == null)return false
        if (a1.length != a2.length)return false;
        for (var i = 0; i < a1.length; i++) {
            if (a1[i] != a2[i])return false;
        }
        return true;
    }
    var createCardsSprites = function (node, array, largeCount) {
        largeCount = largeCount || 7
        if(array.length>largeCount) {
            node.setScale(0.6);
        }else{
            node.setScale(0.9);
        }
        for(var i=0; i<array.length; i++){
            var id = array[i];
            var leftText = null;
            if(_.isArray(array[i])){
                id = array[i][0];
                leftText = new cc.LabelBMFont('剩' + array[i][1] + "张", res.shengyu_fnt);
            }
            var paiName = MaLayer_lnbj.getPaiNameByRowAndId(2, id, true);
            var pai = new cc.Sprite();
            setSpriteFrameByName(pai, paiName, majiang_gs.res.pai0plist);
            node.addChild(pai)
            pai.x = (pai.width) * i + 10
            if(leftText){
                leftText.x = (pai.width) * i + 10;
                leftText.y = -30;
                node.addChild(leftText);
            }
        }

    }

    var BaijiaoLayer = cc.Layer.extend({
        ctor: function (data) {
            this._super();

            var that = this;

            var scene = loadNodeCCSItem(res.BaijiaoLayer_json, that, 0);
            scene.setAnchorPoint(cc.p(0.5, 0.5));
            scene.x = cc.winSize.width / 2;
            scene.y = cc.winSize.height / 2;
            // this.addChild(scene.node);

            $ = create$(this.getChildByName("Layer"));

            TouchUtil.setOnclickListener($('root'), function () {

            }, {effect: TouchUtil.effects.NONE});


            var baijiaoArr = data.baijiao || [{"baiPai": [2], "huPai": [2], "chuPai": [25]}, {
                    "baiPai": [4, 4],
                    "huPai": [4],
                    "chuPai": [25]
                }];
            this.baijiaoArr = baijiaoArr
            TouchUtil.setOnclickListener($('root.btn_ok'), function () {
                var canSendCards = []
                var arr = maLayer.getUpPaiArr();

                arr = arr.sort(compareTwoNumbers);
                for (var key in baijiaoArr) {
                    var cardsObj = baijiaoArr[key];
                    if (isArraySame(cardsObj.baiPai, arr)) {
                        canSendCards = cardsObj.chuPai;
                    }
                }

                if (that.check(arr)) {
                    // var sendData = []
                    // for (var i = 0; i < canSendCards.length; i++) {
                    //     var id = parseInt(canSendCards[i]);
                    //     sendData.push({i: [id]})
                    // }
                    maLayer.baijiaoLastStep(canSendCards, arr)
                } else {
                    showMessageBox("选择错误,请重新选择")
                }

                $('root.btn_ok').setVisible(maLayer.getBaijiaoStep() != 2)
            });
            TouchUtil.setOnclickListener($('root.btn_back'), function () {

                // that.refreshChooseUpPais([1]);
                that.removeFromParent()
                maLayer.baijiaoLayer = undefined;
                maLayer.refreshSelfPaiArr();
                network.recv({'code': 4062, data: null})
                network.recv({'code': 4005, data: null})
                //maLayer._onChiPengGangHuTing()
            });
            $('root.btn_ok').setVisible(false)


            //初始化自动摆牌界面
            this.initAutoView(baijiaoArr);
        },
        initAutoView: function (baijiaoArr) {
            var that = this;
            $("root").setVisible(false);
            TouchUtil.setOnclickListener($('autoroot.btn_shoudong'), function () {
                $('autoroot').removeAllChildren();
                $("root").setVisible(true)
                $("autoroot").setVisible(false);
                cc.log("touch------btn_shoudong");

            });
            TouchUtil.setOnclickListener($('autoroot.btn_back'), function () {
                cc.log("touch------btn_back");
                that.removeFromParent()
                maLayer.baijiaoLayer = undefined;
                maLayer.refreshSelfPaiArr();
                network.recv({'code': 4062, data: null})
                network.recv({'code': 4005, data: null})
            });
            if (baijiaoArr) {
                for (var i = 0; i < baijiaoArr.length; i++) {
                    var obj = baijiaoArr[i];
                    var item = duplicateNode($('item'));
                    if (obj.baiPai) {
                        createCardsSprites(item.getChildByName('baiNode'), obj.baiPai);
                    }
                    if (obj.huPai) {
                        createCardsSprites(item.getChildByName('huNode'), obj.huPai, 5);
                    }
                    $('autoroot.listview').pushBackCustomItem(item);
                    (function (obj,item) {
                        TouchUtil.addScrollTouchListener(item,null,function () {
                            cc.log(obj);
                            $('autoroot').setVisible(false);
                            $('autoroot').removeAllChildren();
                            $('root').setVisible(true);
                            that.refreshChooseUpPais(obj.baiPai)
                            maLayer.baijiaoLastStep(obj.chuPai, obj.baiPai)
                            $('root.btn_ok').setVisible(maLayer.getBaijiaoStep() != 2)
                        })
                    })(obj, item)

                }
            }
        },
        refreshChooseUpPais: function (pais) {
            var initArr = pais || maLayer.getUpPaiArr();
            $('root.cards').removeAllChildren();
            if (initArr.length == 0) {
                $('root.btn_ok').setVisible(false)
                return;
            }
            if (maLayer.getBaijiaoStep() == 2) {
                $('root.btn_ok').setVisible(false)
                return;
            }
            initArr.sort(compareTwoNumbers);
            var arr = null;

            for (var key in this.baijiaoArr) {
                var cardsObj = this.baijiaoArr[key];
                if (isArraySame(cardsObj.baiPai, initArr)) {
                    //arr = cardsObj['huPai']
                    arr = [];
                    for(var i=0; i<cardsObj['huPai'].length; i++){
                        arr.push(cardsObj['huPai'][i][0]);
                    }
                    break;
                }
            }
            // var arr = _.uniq(initArr)
            if (arr) {
                var xoff = 70;
                var startX = -(arr.length - 1) * xoff / 2
                for (var i = 0; i < arr.length; i++) {
                    var id = arr[i];
                    var paiName = MaLayer_lnbj.getPaiNameByRowAndId(2, id, true);
                    var pai = new cc.Sprite();
                    setSpriteFrameByName(pai, paiName, majiang_gs.res.pai0plist);
                    $('root.cards').addChild(pai)
                    pai.x = startX + xoff * i
                }
            }


            //判断是否合法
            if (this.check(initArr)) {
                $('root.btn_ok').setVisible(true)
            } else {
                $('root.btn_ok').setVisible(false)
            }
        },
        check: function (upPaiArr) {
            var baijiaoArr = this.baijiaoArr || {}
            var canSendCards = []
            var arr = upPaiArr;
            arr = arr.sort(compareTwoNumbers);
            for (var key in baijiaoArr) {
                var cardsObj = baijiaoArr[key];
                if (isArraySame(cardsObj.baiPai, arr)) {
                    canSendCards = cardsObj.chuPai;
                }
            }
            return canSendCards.length > 0
        }
    });

    exports.BaijiaoLayer = BaijiaoLayer;
})(this);
